#!/bin/sh

if [ -z "${1}" ] || [ "${1}" == "--help" ] || [ "${1}" == "-h" ]; then
    echo "Usage: sysinfo [OPTION]..."
    echo "  -h, --help"
    echo "      display help and exit"
    echo "  -p, --priority"
    echo "      report a snapshot of the current processes which"
    echo "      contains process id, name, utime, stime and static priority"

    exit 0
fi

if [ "${1}" == "-p" ] || [ "${1}" == "--priority" ]; then
    PROCS=`ls /proc | grep '^[0-9]' | sort -g`
    printf "%6s %20s %12s %12s %20s" "pid" "name" "utime" "stime" "static priority"
    echo
    for proc in ${PROCS}; do
        if [ -d /proc/${proc} ]; then
            stat=/proc/${proc}/stat
            fields=`cat ${stat} | cut -d' ' -f1,2,14,15,19`
            nice=`echo ${fields} | cut -d' ' -f5`
            prio=`expr ${nice} + 120`
            printf "%6d %20s %12d %12d %20d"       \
                `echo ${fields} | cut -d' ' -f1`   \
                "`echo ${fields} | cut -d' ' -f2`" \
                `echo ${fields} | cut -d' ' -f3`   \
                `echo ${fields} | cut -d' ' -f4`   \
                `echo ${prio}`
            echo
        fi
    done

    exit 0
fi

